package com.cwb.study.springbootstudy.controller;

import com.cwb.study.springbootstudy.annotation.PassToken;
import com.cwb.study.springbootstudy.pojo.User;
import com.cwb.study.springbootstudy.service.TokenService;
import com.cwb.study.springbootstudy.service.UserService;
import com.cwb.study.springbootstudy.annotation.UserLoginToken;
import com.cwb.study.springbootstudy.utill.StudyResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：chenwenbin
 * @date ：Created in 2019/08/12 10:58
 * @description：用户接口
 */
@RestController
@RequestMapping("api")
public class UserApi {
    @Autowired
    UserService userService;
    @Autowired
    TokenService tokenService;
    //登陆
    @PostMapping("/login")
    public Object login(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        User userForBase = userService.findUserById(user.getId());
        if (userForBase == null){
            return jsonObject.put("message","登陆失败,用户不存在");
        }else {
            if (!userForBase.getPassword().equals(user.getPassword())){
                jsonObject.put("message","登陆失败,密码错误");
                return jsonObject;
            }else {
                String token = tokenService.getToken(userForBase);
                jsonObject.put("token", token);
                jsonObject.put("user", userForBase);
                return jsonObject;
            }
        }
    }
    //添加认证
    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "您已经通过验证";
    }


    @GetMapping("hello")
    public Object hello(){
        return StudyResult.ok();
    }
}
